package com.dg11185.nearshop.baidumap;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.nearshop.R;
import com.baidu.mapapi.map.BaiduMap.OnMapClickListener;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.model.LatLng;
import com.dg11185.nearshop.baidumap.ReverseGeocode.ReverseListener;

/**
 * 用户导航路线的时候，在地图上自定义选取当前位置界面
 * 
 * @author：Daibo
 * @time：2014-5-11 下午3:06:46
 */
public class ChoicePositionActivity extends BaseShopActivity {

    private TextView poiTxt;

    private LinearLayout dataLayout, loadingLayout;

    private LatLng curGeoPoint; // 用户点击位置

    private ReverseGeocode geocode = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initShopOverlay();
        poiTxt = (TextView) this.findViewById(R.id.map_choice_position);
        dataLayout = (LinearLayout) this.findViewById(R.id.map_choice_layout);
        loadingLayout = (LinearLayout) this.findViewById(R.id.map_choice_loading);

        mBaiduMap.setOnMapClickListener(listener);
        geocode = new ReverseGeocode(reverseListener);
    }

    @Override
    public void setContentView() {
        setContentView(R.layout.map_user_choice_position);
    }

    private ReverseListener reverseListener = new ReverseListener() {

        @Override
        public void onGetAddrResult(String str) {
            loadingLayout.setVisibility(View.GONE);
            dataLayout.setVisibility(View.VISIBLE);
            poiTxt.setText(str);
        }
    };

    private OnMapClickListener listener = new OnMapClickListener() {

        @Override
        public boolean onMapPoiClick(MapPoi arg0) {
            return false;
        }

        @Override
        public void onMapClick(LatLng arg0) {

            loadingLayout.setVisibility(View.VISIBLE);
            dataLayout.setVisibility(View.INVISIBLE);
            curGeoPoint = arg0;
            geocode.reverseGeoCode(arg0);
            addOverlay(arg0, R.drawable.map_current_poi);
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    public void onSubmitClicked(View v) {
        Intent intent = new Intent();
        /** 用户是否有选择自定义位置 */
        if (curGeoPoint == null) {
            intent.putExtra("IS_CHOICED", false);
        } else {
            intent.putExtra("IS_CHOICED", true);
            intent.putExtra("GEO_CONTENT", poiTxt.getText());
            intent.putExtra("GEO_POINT", new double[] { curGeoPoint.latitude, curGeoPoint.longitude });
        }
        this.setResult(RESULT_OK, intent);
        this.finish();
    }

}
